package com.ekalenchuk.module.dto;

import java.util.Collection;
import java.util.EnumSet;
import java.util.Set;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.ekalenchuk.module.domain.user.User;
import com.ekalenchuk.module.dto.session.UserSession;
import com.ekalenchuk.module.enums.Authority;

public class UserDetailsInfo implements UserDetails {

	private static final long serialVersionUID = -3605161789840593743L;

	private Long id;

	private String identificationName;

	private String password;
	
	private Long binaryAuthorities;

	private Set<Authority> authorities;
	
	private UserSession userSession;
	
	public Collection<? extends GrantedAuthority> getAuthorities() {
		authorities = EnumSet.noneOf(Authority.class);
		for (Authority authority : Authority.values())
			if ((binaryAuthorities & (1 << authority.ordinal())) != 0)
				authorities.add(authority);
		return authorities;
	}

	public void setAuthority(Set<Authority> authorities) {
		binaryAuthorities = 0L;
		for (Authority authority : authorities)
			binaryAuthorities |= 1 << authority.ordinal();
	}

	public String getPassword() {
		return password;
	}

	public String getUsername() {
		return identificationName;
	}

	public boolean isAccountNonExpired() {
		return true;
	}

	public boolean isAccountNonLocked() {
		return true;
	}

	public boolean isCredentialsNonExpired() {
		return true;
	}

	public boolean isEnabled() {
		return true;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getIdentificationName() {
		return identificationName;
	}

	public void setIdentificationName(String identificationName) {
		this.identificationName = identificationName;
	}

	public Long getBinaryAuthorities() {
		return binaryAuthorities;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public UserSession getUserSession() {
		return userSession;
	}

	public void setUserSession(UserSession userSession) {
		this.userSession = userSession;
	}

}